package com.yeeKa.common.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.yeeKa.enums.ResTypeEnum;
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@ToString
public class Result<T> {

    private String code;

    private String msg;

    private T data;

    public Result<T>  success(T t){
        this.code = ResTypeEnum.SUCCESS.getCode();
        this.msg = ResTypeEnum.SUCCESS.getDesc();
        this.data = t;
        return this;
    }

    public Result<T> success(){
        this.code = ResTypeEnum.SUCCESS.getCode();
        this.msg = ResTypeEnum.SUCCESS.getDesc();
        return this;
    }
    public Result<T> failure(String msg){
        this.code = ResTypeEnum.FAILURE.getCode();
        this.msg = msg;
        return this;
    }
    public Result<T> failure(String code,String msg){
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> failure(Exception e){
        this.code = ResTypeEnum.FAILURE.getCode();
        this.msg = e.getMessage();
        return this;
    }

    public static <T> Result<T> SUCCESS(T t) {
        return Result.<T>builder().data(t).code(ResTypeEnum.SUCCESS.getCode()).msg(ResTypeEnum.SUCCESS.getDesc()).build();
    }

    public static <T> Result<T> SUCCESS() {
        return Result.<T>builder().code(ResTypeEnum.SUCCESS.getCode()).msg(ResTypeEnum.SUCCESS.getDesc()).build();
    }

    public static <T> Result<T> FAILURE(String msg) {
        return Result.<T>builder().code(ResTypeEnum.FAILURE.getCode()).msg(msg).build();
    }

    public static <T> Result<T> FAILURE(Exception e) {
        return Result.<T>builder().code(ResTypeEnum.FAILURE.getCode()).msg(e.getMessage()).build();
    }

}
